/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agenda;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;

/**
 *
 * @author John
 */
public class MiFrame {
    
    ArrayList<Cita> citas = new ArrayList<Cita>();
    JFrame frmPrincipal = new JFrame();
    JTextField txtNuevaCita = new JTextField();
    JSpinner spnHoraCita = new JSpinner(new SpinnerDateModel(Calendar.getInstance().getTime(), null, null, Calendar.HOUR_OF_DAY));
    JPanel pnlEventos = new JPanel();
    JPanel pnlCalendario = new JPanel();
    JButton btnAnterior = new JButton("<");
    JButton btnSiguiente = new JButton(">");
    JButton btnAnnadir = new JButton("Añadir");
    JButton btnEliminar = new JButton("Eliminar");
    JLabel lblSelected;
    JList listaCitas = new JList();
    String[] meses = {"Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"};
    int[] dias = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int dia = Calendar.getInstance().get(Calendar.DATE);
    int mes = Calendar.getInstance().get(Calendar.MONTH);
    int año = Calendar.getInstance().get(Calendar.YEAR);
    
    /**
     * Método constructor
     */
    
    public MiFrame() {
        
        JLabel lbl = new JLabel("Nueva entrada:");
        lbl.setBounds(25, 25, 100, 23);
        lbl.setVerticalAlignment(JLabel.CENTER);
        
        frmPrincipal.getContentPane().add(lbl);
        
        txtNuevaCita.setBounds(150, 25, 300, 23);
        
        spnHoraCita.setBackground(Color.white);
        spnHoraCita.setBorder(BorderFactory.createLineBorder(Color.gray));
        spnHoraCita.setBounds(150, 60, 60, 23);
        spnHoraCita.setEditor(new JSpinner.DateEditor(spnHoraCita, "HH:mm"));
        spnHoraCita.setLayout(null);
        spnHoraCita.getComponent(0).setBounds(40, 0, 20, 12);
        spnHoraCita.getComponent(1).setBounds(40, 11, 20, 12);
        spnHoraCita.getComponent(2).setBounds(5, 1, 35, 21);
        
        btnAnterior.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                
                mes--;
                
                if (mes < 0){
                 
                    mes = 11;
                    año--;
                    
                }
                
                calendario();
                
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        btnAnterior.setBorder(BorderFactory.createLineBorder(Color.black));
        btnAnterior.setBounds(1, 1, 39, 29);
        btnAnterior.setMargin(new Insets(5, 5, 5, 5));
        
        btnSiguiente.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                
                mes++;
                
                if (mes > 11){
                 
                    mes = 0;
                    año++;
                    
                }
                
                calendario();
                
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        btnSiguiente.setBorder(BorderFactory.createLineBorder(Color.black));
        btnSiguiente.setBounds(240, 1, 39, 29);
        btnSiguiente.setMargin(new Insets(5, 5, 5, 5));
        
        pnlCalendario.add(btnAnterior);        
        pnlCalendario.add(btnSiguiente);
        pnlCalendario.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                
                if (lblSelected != null){
                    
                    lblSelected.setBackground(null);
                    lblSelected.setOpaque(false);
                
                }
                
                if (e.getPoint().y >= 30) {
                
                    lblSelected = (JLabel)pnlCalendario.getComponentAt(e.getPoint());
                    
                    if (!lblSelected.getText().isEmpty()) {
                        
                        lblSelected.setBackground(Color.decode("#8888ff"));
                        lblSelected.setOpaque(true);
                        dia = Integer.parseInt(lblSelected.getText());
                    
                    }
                
                }
                
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        pnlCalendario.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        pnlCalendario.setBackground(Color.white);
        pnlCalendario.setBounds(500, 25, 280, 30);
        pnlCalendario.setLayout(null);
        
        btnAnnadir.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                
                Date c = (Date)spnHoraCita.getModel().getValue();
                String hh = c.getHours() + "";
                String mm = c.getMinutes() + "";
                
                if (hh.length() == 1)
                    
                    hh = 0 + hh;
                
                if (mm.length() == 1)
                    
                    mm = 0 + mm;
                
                citas.add(new Cita(hh + ":" + mm, dia + "/" + (mes + 1) + "/" + año, txtNuevaCita.getText()));
                
                Vector v = new Vector();
                
                for (int i = 0; i < citas.size(); i++) {
                
                    v.add(citas.get(i).MostrarCita());
                    
                }
                
                listaCitas.removeAll();
                listaCitas.setListData(v);
                
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
            
        });
        btnAnnadir.setBounds(500, 80, 280, 30);
        
        btnEliminar.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                
                if (!listaCitas.isSelectionEmpty()) {
                    
                    citas.remove(listaCitas.getSelectedIndex());

                    Vector v = new Vector();

                    for (int i = 0; i < citas.size(); i++) {

                        v.add(citas.get(i).MostrarCita());

                    }

                    listaCitas.removeAll();
                    listaCitas.setListData(v);
                
                }
                
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
            
        });
        btnEliminar.setBounds(500, 120, 280, 30);
        
        listaCitas.setBorder(BorderFactory.createLineBorder(Color.gray));
        listaCitas.setBounds(25, 100, 425, 475);
        listaCitas.setOpaque(true);
        
        frmPrincipal.getContentPane().add(listaCitas);
        frmPrincipal.getContentPane().add(pnlCalendario);
        frmPrincipal.getContentPane().add(txtNuevaCita);
        frmPrincipal.getContentPane().add(spnHoraCita);
        frmPrincipal.getContentPane().add(btnAnnadir);
        frmPrincipal.getContentPane().add(btnEliminar);
        frmPrincipal.getContentPane().setLayout(null);
        frmPrincipal.getContentPane().setPreferredSize(new Dimension(800, 600));
        
        frmPrincipal.pack();
        frmPrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmPrincipal.setVisible(true);
        
        calendario();
        
    }
    
    /**
     * Genera un calendario para el panel del frame
     */
    
    private void calendario() {
        
        for (int i = (pnlCalendario.getComponentCount() - 1); i > 1; i--) {
            
            pnlCalendario.remove(i);
        
        }
        
        pnlCalendario.setSize(280, 30);
        btnAnnadir.setLocation(500, 80);
        btnEliminar.setLocation(500, 120);
        
        if (mes == 1 && esBisiesto(año))
            
            dias[1] = 29;
            
        else
            
            dias[1] = 28;
        
        int d = dds();
        
        JLabel lbl = new JLabel(meses[mes] + " (" + año + ")");
        lbl.setBorder(BorderFactory.createLineBorder(Color.black));
        lbl.setBounds(40, 0, 200, 30);
        lbl.setHorizontalAlignment(JLabel.CENTER);
        lbl.setVerticalAlignment(JLabel.CENTER);
        
        pnlCalendario.add(lbl);
        
        int i;
        
        i = 0 - d;
        
        for (int y = 0; y <= Math.floor((d + dias[mes] - 1) / 7); y++) {
        
            pnlCalendario.setSize(280, pnlCalendario.getHeight() + 30);
            btnAnnadir.setLocation(500, btnAnnadir.getLocation().y + 30);
            btnEliminar.setLocation(500, btnEliminar.getLocation().y + 30);
            
            for (int x = 0; x < 7; x++) {
                
                i++;
                
                if (i > 0 && i <= dias[mes])
                
                    lbl = new JLabel(i + "");

                else

                    lbl = new JLabel();
                
                if (i == dia) {
                    
                    lblSelected = lbl;
                    lbl.setBackground(Color.decode("#8888ff"));
                    lbl.setOpaque(true);
                    
                }
                
                lbl.setBorder(BorderFactory.createLineBorder(Color.black));
                lbl.setBounds(x * 40, (y * 30) + 30, 40, 30);
                lbl.setHorizontalAlignment(JLabel.CENTER);
                lbl.setVerticalAlignment(JLabel.CENTER);
        
                pnlCalendario.add(lbl);
                

            }
            
        }
        
        pnlCalendario.repaint();
        
    }
 
    /**
     * Recibe el año y calcula si es bisiesto
     * 
     * @param año El año a comprobar
     * @return true si es bisiesto y false si no lo es
     */
    private boolean esBisiesto(int año) {
        
        return ((año % 4 == 0 && año % 100 != 0) || año % 400 == 0);
        
    }
    
    /**
     * calcula el primer día de la semana del mes
     * 
     * @return el número del primer día de la semana del mes
     */
    
    private int dds() {
        
        Calendar calendario = Calendar.getInstance();

        calendario.set(año, mes, 1);
        
        int d = calendario.get(Calendar.DAY_OF_WEEK) - 2;
        
        if (d < 0)
            
            d = 6;
        
        return d;
        
    }
    
}